//
//  RegisterViewController.m
//  yemeishe1
//
//  Created by 吴坤金 on 2017/2/28.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "RegisterViewController.h"
#import "YMSMemberManager.h"
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *mobileNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordConfirmTextField;

@property (weak, nonatomic) IBOutlet UITextField *verificationTextField;
@property (weak, nonatomic) IBOutlet UIButton *verificationButton;
@property (nonatomic, strong) NSDictionary *responseDict;
@property (nonatomic, assign) int num;

@property (nonatomic, weak) NSTimer *timer;
@end

@implementation RegisterViewController

- (NSDictionary *)responseDict {
    if (!_responseDict) {
        _responseDict = [NSDictionary dictionary];
    }
    return _responseDict;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatVerificationCode) userInfo:nil repeats:YES];
//    [_verificationBut]

}
- (IBAction)backButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(IBAction)getVerificationCode:(id)sender
{
    if (!_mobileNumberTextField.text.length) {
        NSLog(@"没有输入手机号");
        return;
    }
    [_timer setFireDate:[NSDate distantPast]];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:_mobileNumberTextField.text forKey:@"moblie_number"];
    [dic setValue:@"register" forKey:@"type"];
    [dic setValue:[YMSMember loginMember].token forKey:@"_token"];
    [[YMSMemberManager alloc]getVerificationCodeWithDic:dic successBlock:^(NSDictionary *result){
        
    } failureBlock:^(NSString *error) {
        
    }];
}

-(IBAction)onTounchRegister:(id)sender
{
    if (!_mobileNumberTextField.text.length) {
        NSLog(@"没有输入手机号");
        return;
    }
    if (!_passwordTextField.text.length) {
        NSLog(@"没有输入密码");
        return;
    }
    if (!_passwordConfirmTextField.text.length) {
        NSLog(@"没有输入确认密码");
        return;
    }
    if (!_verificationTextField.text.length) {
        NSLog(@"没有输入验证码");
        return;
    }
    if ([_passwordConfirmTextField.text isEqualToString:_verificationTextField.text]    ) {
        NSLog(@"两次输入密码不一致");
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:_mobileNumberTextField.text forKey:@"username"];
    [dic setValue:_passwordTextField.text forKey:@"password"];
    [dic setValue:_passwordConfirmTextField.text forKey:@"password_confirmation"];
    [dic setValue:_verificationTextField.text forKey:@"user_code"];
    [dic setValue:@"1" forKey:@"confirm"];
    [dic setObject:[YMSMember loginMember].token forKey:@"_token"];
    [[YMSMemberManager alloc]MemberRegisterWithDic:dic successBlock:^(NSDictionary *result){
        
    } failureBlock:^(NSString *error) {
        
    }];
}

- (void)repeatVerificationCode {
    
    if (_num > 0) {
        _num--;
        [_verificationButton setTitle:[NSString stringWithFormat:@"%ld秒后重新获取", (long)_num] forState:UIControlStateNormal];
        _verificationButton.userInteractionEnabled = NO;
        return;
    }
    
    if (_num == 0) {
        _num = 60;
        [_timer setFireDate:[NSDate distantFuture]];
        _verificationButton.userInteractionEnabled = YES;
        [_verificationButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}



- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [_timer invalidate];
    _timer = nil;
}




-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    self.view.backgroundColor = [UIColor whiteColor];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
